package com.asia5b.a5mall.home.activity

import android.annotation.SuppressLint
import android.os.Build
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.home.databinding.ActivityWebBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel

import kotlinx.android.synthetic.main.activity_web.*

@Route(path = ActCons.WebActivity)
class WebActivity : SimpleActivity<SimpleViewModel, ActivityWebBinding>() {

    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivityWebBinding {
        ARouter.getInstance().inject(this)
        title = intent.getStringExtra("title") ?: ""
        url = intent.getStringExtra("url") ?: ""
        return ActivityWebBinding.inflate(layoutInflater)
    }
    override var sIsDark = true
    var title: String = ""

    var url: String = ""


    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {

        head_web.setTitleName(title)
        webview.webViewClient = object : WebViewClient() {

        }
        webview.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                if (newProgress == 100) {
                    pb_lay.visibility = View.GONE
                } else {
                    pb_lay.visibility = View.VISIBLE
                    pb_lay.progress = newProgress
                }
                super.onProgressChanged(view, newProgress)
            }
        }
        initWebSettings()
        webview.loadUrl(url)
    }

    fun initWebSettings() {
        val webSettings = webview.getSettings()


        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.javaScriptEnabled = true

        webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口, 设置允许JS弹窗
        webSettings.domStorageEnabled = true//DOM Storage缓存

        //支持插件
        webSettings.pluginState = WebSettings.PluginState.ON

        //设置自适应屏幕，两者合用（下面这两个方法合用）
        webSettings.useWideViewPort = true //将图片调整到适合webview的大小
        webSettings.loadWithOverviewMode = true // 缩放至屏幕的大小
        webSettings.textZoom = 100 // 就可以禁止缩放，按照百分百显示。

        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
        webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.displayZoomControls = false //隐藏原生的缩放控件

        //其他细节操作
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE //关闭webview中缓存
        webSettings.allowFileAccess = true //设置可以访问文件

        webSettings.loadsImagesAutomatically = true //支持自动加载图片
        webSettings.defaultTextEncodingName = "utf-8"//设置编码格式

        webSettings.setAppCacheEnabled(false)//是否使用缓存
        webSettings.blockNetworkImage = false

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
        }
    }

    override fun initEvent() {
    }
    override fun initData() {

    }

    fun getWebView():WebView{
        return webview
    }

    override fun onBackPressed() {
        onBack(getWebView())
        //        super.onBackPressed();
    }

    protected fun onBack(webView: WebView) {
        if (webView.canGoBack()) {
            webView.goBack()
        } else {
            finish()
        }
    }
}